<?php if ( ! defined('WDK_PATH')) exit('No direct script access allowed');
require_once PROJECT_PATH.'core/AdminController.php';
class User extends AdminController
{
	function __construct()
	{
		parent::__construct();
	}
	
	protected function _init() 
	{
		parent::_loginCheck();
	}
	
	public function index()
	{	
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$assign_data = array();
		/****************************************************************************************************************************/
		try
		{
			$assign_data['json'] = $this->json(true);
		}
		catch(WException $e)
		{
			parent::_error($e);
		}
		/*****************************************************************************************************************************
		* RESULT
		*****************************************************************************************************************************/
		parent::_view($assign_data);
		/****************************************************************************************************************************/
	}
	
	public function json($return=false)
	{
		/*****************************************************************************************************************************
		 * POST PARAMS..
		*****************************************************************************************************************************/
		$page	= (int)		post('page', 1);
		$sf		= (string)	post('sf', '');
		$sk		= (string)	post('sk', '');
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$listsize		= 15;
		$pagesize		= 10;
		$lm = Wdk::get('UserManager');
		$lists = array();
		$where = array();
		$assign_data = array('listsize'=>$listsize,'pagesize'=>$pagesize);
		/****************************************************************************************************************************/
		try
		{
			if(strlen($sf) > 0 && strlen($sk) > 0)
			{
				$where[] = "`{$sf}` LIKE '{$sk}'";
			}
			$lm['listsize'] = $listsize;
			$lm['page'] = $page;
			$lm['where'] = $where;
			$lm->load();
			foreach($lm['lists'] as $idx=>$list)
			{
				$data = array(	'no'			=>	$lm['total'] - ($page-1)*$listsize - $idx,
								'seq'			=>	$list['user_seq'],
								'login_id'		=>	$list['user_login_id'],
								'name'			=>	$list['user_name'],
								'sex'			=>	($list['user_sex'] == USER_SEX_FEMALE) ? '여성' : '남성',
								'mobile'		=>	$list['user_mobile'],
								'email'			=>	$list['user_email'],
								'insert_time'	=>	date('Y-m-d H:i:s', $list['user_insert_time']),
								'last_login'	=>	($list['user_last_login'] > 0) ? date('Y-m-d H:i:s', $list['user_last_login']) : '-'
							);
				array_push($lists, $data);
			}
			$assign_data['total'] = $lm['total'];
			$assign_data['lists'] = $lists;
		}
		catch(NoRecordException $e)
		{
			$assign_data['total'] = 0;
			$assign_data['lists'] = array();
		}
		catch(WException $e)
		{
			$assign_data['error'] = true;
			$assign_data['msg'] = "Error";
		}
		/*****************************************************************************************************************************
		 * RESULT
		*****************************************************************************************************************************/
		return parent::_json($assign_data, $return);
		/****************************************************************************************************************************/
	}
	
	public function edit($seq=0) 
	{
		$this->__edit();
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$assign_data = array();
		$obj = Wdk::get('User');
		/****************************************************************************************************************************/
		try
		{
			if((int)$seq > 0)
			{
				$user->load($seq);
			}
			$assign_data['data'] = $obj;
			$user_group = Wdk::get('UserGroupManager');
			$user_group->loadAll();
			$assign_data['user_group'] = $user_group['lists'];
		}
		catch(NoRecordException $e)
		{
			parent::_error($e);
		}
		catch(WException $e)
		{
			parent::_error($e);
		}
		/*****************************************************************************************************************************
		* RESULT
		*****************************************************************************************************************************/
		parent::_layout('admin_popup');
		parent::_view($assign_data);
		/****************************************************************************************************************************/
	}
	
	private function __edit() 
	{
		if(parent::_isPost() == false) return;
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$assign_data = array();
		$obj = Wdk::get('User');
		/****************************************************************************************************************************/
		try
		{
			$seq = post('user_seq', 0);
			if($seq > 0)
			{
				$obj->load($seq);
			}
			
			$user_identity_no	 = (array) post('user_identity_no');
			$user_birth_date	 = (array) post('user_birth_date');
			$user_phone			 = (array) post('user_phone');
			$user_mobile		 = (array) post('user_mobile');
			$user_new_passwd	= (string) post('user_new_passwd'); 
			$user_new_passwd2	= (string) post('user_new_passwd2');
			
			$obj['user_login_id']		= (string) post('user_login_id');
			$obj['fk_user_group_seq']	= (int) post('fk_user_group_seq');
			$obj['user_name']			= (string) post('user_name');
			$obj['user_nick']			= (string) post('user_nick');
			$obj['user_identity_no']	= implode("-", $user_identity_no);
			$obj['user_sex']			= (string) post('user_sex'); 
			$obj['user_birth_date']		= implode("-", $user_birth_date); 
			$obj['user_phone']			= implode("-", $user_phone); 
			$obj['user_mobile']			= implode("-", $user_mobile);
			$obj['user_email']			= (string) post('user_email');
			if($user_new_passwd && $user_new_passwd == $user_new_passwd2 )
			{
				$obj['user_login_passwd']	= $user_new_passwd;
			} 
			$obj->autoUpdate();
		}
		catch(NoRecordException $e)
		{
			parent::_error($e);
		}
		catch(WException $e)
		{
			parent::_error($e);
		}
		/*****************************************************************************************************************************
		* RESULT
		*****************************************************************************************************************************/
		parent::_script(array(	"alert('저장완료!')",
								"opener.wlm.reload();",
								"window.close();"
							)
						);
		/****************************************************************************************************************************/		
	}

	public function del() 
	{
		parent::_del(Wdk::get('User'));
	}
}